package com.hw.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/*@WebFilter(filterName = "imgFilter",urlPatterns = "/*")*/
public class imgFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /*防盗链，通过头部来判断*/

        HttpServletRequest request= (HttpServletRequest) req;


        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() ;



        String fromUrl = request.getHeader("referer");

        if (fromUrl==null || fromUrl.startsWith(basePath)){
            chain.doFilter(req, resp);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
